package com.li.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Utils {

    public static List<Person> personOrder() {
        List<Person> personList = new ArrayList<>();
        Person person = new Person(1, null, 1, "李爷爷");
        Person person2 = new Person(2, null, 1, "王爷爷");

        Person person3 = new Person(3, 1, 2, "李爸爸");
        Person person4 = new Person(4, 2, 2, "王爸爸");

        Person person5 = new Person(5, 3, 3, "李儿子");
        Person person6 = new Person(6, 4, 3, "王儿子");

        Person person7 = new Person(7, 5, 4, "李孙子");
        Person person8 = new Person(8, 6, 4, "王孙子");


        Person person9 = new Person(9, 1, 2, "李爸爸弟弟");
        Person person10 = new Person(10, 2, 2, "王爸爸哥哥");

        personList.add(person);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);
        personList.add(person5);
        personList.add(person6);
        personList.add(person7);
        personList.add(person8);
        personList.add(person9);
        personList.add(person10);

        List<Person> personListTemp = personList.stream().filter(p -> {
            return p.getOrderNum() == 1;
        }).map(p -> {
            p.setSonList(getSonListByPerson(p, personList));
            return p;
        }).collect(Collectors.toList());

        personListTemp.forEach(s -> System.out.println(s));
        return personListTemp;
    }

    /**
     * 获取每个人的下一代
     *
     * @param p          父亲
     * @param personList 儿子集合
     * @return
     */
    private static List<Person> getSonListByPerson(Person p, List<Person> personList) {
        List<Person> collect = personList.stream().filter(son -> {
            return p.getId() == son.getPersonId();
        }).map(son -> {
            son.setSonList(getSonListByPerson(son, personList));
            return son;
        }).collect(Collectors.toList());
        return collect;
    }
}
